'''
Author: sky 1575951471@qq.com
Date: 2023-12-17 14:40:57
LastEditors: sky 1575951471@qq.com
LastEditTime: 2023-12-17 17:45:38
FilePath: \create_port\CreatePort.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import os
import xlrd3
import dhnLib as dl
import xml.etree.ElementTree as ET

def GetFiles():
    workspace = os.getcwd().replace('\\','/') + '/'
    xlsxDir = dl.FindFilePath(workspace, '*.xlsx')
    outpArxmlDir = dl.FindFilePath(workspace, 'OUTP_SWC.arxml')
    return xlsxDir, outpArxmlDir

def ExtractSpecInfoLostFromExcel(xlsxDir):
    specInfoDicList = []
    xlsxBook = xlrd3.open_workbook(xlsxDir)
    sheetInfo = xlsxBook.sheet_by_index(0)
    #获取表格的总行数
    totalRow = sheetInfo.nrows                      
    if (sheetInfo.cell_value(0, 0) == 'signal_name'):
        for row in range(totalRow - 1):
            #将signal的name 填充到列表specInfoDicList中
            specInfoDicList.append(sheetInfo.cell_value(row +1, 0))
    return specInfoDicList

def InsertPPortToOutp(siglist):
    tree = ET.parse(outpArxmlDir)
    root = tree.getroot()
    pPortTemp = '''                 <P-PORT-PROTOTYPE>
              <SHORT-NAME>PP_$SIG$</SHORT-NAME>
              <PROVIDED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/SUM_SSC/PortInterfaces/IF_$SIG$</PROVIDED-INTERFACE-TREF>
            </P-PORT-PROTOTYPE>\n'''
    portsNode = root.find('.//{http://autosar.org/schema/r4.0}PORTS')
    for siglistname in siglist:
        pPortStr = pPortTemp.replace('$SIG$', siglistname)
        pPortNode = ET.fromstring(pPortStr)
        portsNode.append(pPortNode)
        tmpList = list(portsNode)
        tmpList[-1].tail = '\n      '

    print('port num : ',len(portsNode))
    dl.WriteArxml(tree, 'OUTP_SWCTMP.arxml')    
    

if __name__ == '__main__':
    xlsxDir, outpArxmlDir = GetFiles()
    siglist = ExtractSpecInfoLostFromExcel(xlsxDir)
    print('sig num:',len(siglist))
    InsertPPortToOutp(siglist)

         
    

